<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 差的绝对值为 K 的数对数目 -->
    <script>
      //双重for循环找差值等于k的
      function countKDifference(nums, k) {
        let a = 0
        for (let i = 0; i < nums.length - 1; i++) for (let j = i + 1; j < nums.length; j++) if (Math.abs(nums[j] - nums[i]) === k) a++
        return a
      }
      console.log(countKDifference([1, 2, 2, 1], 1))
    </script>
  </body>
</html>
